package com.lanju.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanju.common.SnowflakeIdWorker;
import com.lanju.common.SpringUtils;
import com.lanju.entity.User;
import com.lanju.entity.ZukeContract;
import com.lanju.service.ZuKeContractService;

@Controller
@RequestMapping("zukeContract")
public class ZuKeContractController {
	
	@Autowired
	ZuKeContractService zuKeContractService;
	
	/**
	 * 创建合同
	 * 作者：zengqd
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("createContract")
	@ResponseBody
	public Map<String,Object> createContract(HttpServletRequest request ,HttpServletResponse response,HttpSession session){
		 Map<String, Object> param = SpringUtils.getParameterMap(request);
		 User user =(User) session.getAttribute("userModel");
		 JSONObject json = JSONObject.fromObject(param);
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		 ZukeContract contract=(ZukeContract)JSONObject.toBean(json, ZukeContract.class);
		 contract.setCreate_time(new Date());
		 contract.setUpdate_time(new Date());
		 SnowflakeIdWorker idWorker = new SnowflakeIdWorker(RandomUtils.nextLong(0,31), RandomUtils.nextLong(0,31));
		 contract.setContract_id("HT-"+idWorker.nextId());
		 contract.setCreator(user.getUserId());
		 contract.setSign_time(new Date());
		 contract.setStatus(-1);
			try {
				 if(param.get("renter_start_time") !=null)contract.setRenter_start_time(sdf.parse(param.get("renter_start_time").toString()));
				 if(param.get("renter_end_time") !=null)contract.setRenter_end_time(sdf.parse(param.get("renter_end_time").toString()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		return zuKeContractService.createContract(contract);
	}
	/**
	 * 更新合同
	 * 作者：zengqd
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 */
	@RequestMapping("updateConstract")
	@ResponseBody
	public Map<String,Object> updateConstract(HttpServletRequest request ,HttpServletResponse response,HttpSession session){
		 Map<String, Object> param = SpringUtils.getParameterMap(request);
		 User user =(User) session.getAttribute("userModel");
		 JSONObject json = JSONObject.fromObject(param);
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		 ZukeContract contract=(ZukeContract)JSONObject.toBean(json, ZukeContract.class);
		 SnowflakeIdWorker idWorker = new SnowflakeIdWorker(RandomUtils.nextLong(0,31), RandomUtils.nextLong(0,31));
		 contract.setUpdate_time(new Date());
		 contract.setContract_id(param.get("contractId").toString());
			try {
				 if(param.get("renter_start_time") !=null)contract.setRenter_start_time(sdf.parse(param.get("renter_start_time").toString()));
				 if(param.get("renter_end_time") !=null)contract.setRenter_end_time(sdf.parse(param.get("renter_end_time").toString()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		return zuKeContractService.updateConstract(contract);
	}
	
	/**
	 * 获取合同详情
	 * @return
	 */
	@RequestMapping("/getConstractDetailById")
	@ResponseBody
	public Map<String,Object> getConstractDetailById(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		User user =(User) session.getAttribute("userModel");
		if(user !=null){
			param.put("flag", user.getFlag());
			param.put("login", true);
		}else{
			param.put("flag","0");
			param.put("login", false);
		}
		return zuKeContractService.getConstractDetailById(param);
	}
	
	/**
	 * 删除合同
	 * @param request
	 * @return
	 */
	@RequestMapping("/delContract")
	@ResponseBody
	public Map<String,Object> delContract(HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		return zuKeContractService.delContract(param);
	}
	
	/**
	 * 获取合同列表
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/getContractList")
	@ResponseBody
	public Map<String,Object> getContractList(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		User user =(User) session.getAttribute("userModel");
		param.put("userId", user.getUserId());
		return zuKeContractService.getContractList(param);
	}
	
	/**
	 * 获取合同列表(管理员)
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/getContractListForAdmin")
	@ResponseBody
	public Map<String,Object> getContractListForAdmin(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		User user =(User) session.getAttribute("userModel");
		if(user !=null){
			param.put("flag", user.getFlag());
		}else{
			param.put("flag","0");
		}
		return zuKeContractService.getContractList(param);
	}
	/**
	 * 签合同
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 */
	@RequestMapping("/signComtract")
	@ResponseBody
	public Map<String,Object> signComtract(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		return zuKeContractService.signComtract(param);
	}
	/**
	 * 支付
	 * 作者：zengqd
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 */
	@RequestMapping("/updatePayStatus")
	@ResponseBody
	public Map<String,Object> updatePayStatus(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		return zuKeContractService.updatePayStatus(param,request,response);
	}
}
